<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="1100px">
    <el-form
      :model="formData"
      ref="formRef"
      :rules="formRules"
      vloading="formLoading"
      label-width="auto"
    >
      <el-row>
        <el-col :span="8">
          <el-form-item label="老人姓名" prop="elderName">
            <el-input v-model="formData.elder.name" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="药品名称" prop="medicineName">
            <el-input v-model="formData.medicine.medicineName" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="药品简码" prop="medicineBrevityCode">
            <el-input v-model="formData.medicine.medicineBrevityCode" disabled />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="药品类型" prop="medicineType">
            <el-select v-model="formData.medicine.medicineType" disabled>
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.HEALTH_MEDICINE_TYPE)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="药品规格" prop="medicineSpecification">
            <el-input v-model="formData.medicine.medicineSpecification" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="药品剂型" prop="medicineDosageForm">
            <el-select v-model="formData.medicine.medicineDosageForm" disabled>
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.HEALTH_MEDICINE_DOSAGE_FORM)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="当前库存" prop="stockNumber">
            <el-input v-model="formData.stockNumber" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="校正类型" prop="adjustType">
            <el-radio-group v-model="formData.adjustType">
              <el-radio :value="HealthMedicineStockRecordTypeEnum.LOSS">
                {{
                  getDictObj(
                    DICT_TYPE.HEALTH_MEDICINE_STOCK_RECORD_TYPE,
                    HealthMedicineStockRecordTypeEnum.LOSS
                  ).label
                }}
              </el-radio>
              <el-radio :value="HealthMedicineStockRecordTypeEnum.ADDITIONAL">
                {{
                  getDictObj(
                    DICT_TYPE.HEALTH_MEDICINE_STOCK_RECORD_TYPE,
                    HealthMedicineStockRecordTypeEnum.ADDITIONAL
                  ).label
                }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="校正数量" prop="adjustNumber">
            <InputNumber
              v-model="formData.adjustNumber"
              placeholder="请输入校正数量"
              type="decimal"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button type="primary" :disabled="formLoading" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup name="HealthMedicineStockAdjust" lang="ts">
import { DICT_TYPE, getDictObj, getIntDictOptions } from '@/utils/dict'
import { HealthMedicineStockRecordTypeEnum } from '@/utils/constants'
import * as HealthMedicineStockApi from '@/api/health/medicineStock'
import { cloneDeep } from 'lodash-es'
const dialogTitle = ref('')
const dialogVisible = ref(false)
const formRef = ref()
const formLoading = ref(false)
const message = useMessage()
const formData = ref({
  id: '',
  elderId: '',
  medicineId: '',
  elder: {
    name: ''
  },
  medicine: {
    medicineName: '',
    medicineBrevityCode: '',
    medicineType: '',
    medicineSpecification: '',
    medicineDosageForm: ''
  },
  stockNumber: '',
  adjustType: '',
  adjustNumber: 0
})
const formRules = reactive({
  adjustType: [{ required: true, message: '请选择校正类型', trigger: 'change' }],
  adjustNumber: [{ required: true, message: '请输入校正数量', trigger: 'blur' }]
})
/** 打开弹窗 */
const open = (row) => {
  dialogVisible.value = true
  resetForm()
  formData.value = cloneDeep(row)
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: '',
    elderId: '',
    medicineId: '',
    elder: {
      name: ''
    },
    medicine: {
      medicineName: '',
      medicineBrevityCode: '',
      medicineType: '',
      medicineSpecification: '',
      medicineDosageForm: ''
    },
    stockNumber: '',
    adjustType: '',
    adjustNumber: 0
  }

  formRef.value?.resetFields()
}

const emit = defineEmits(['success'])
/** 提交表单 */
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    await HealthMedicineStockApi.adjust({
      id: formData.value.id,
      number: formData.value.adjustNumber,
      adjustType: formData.value.adjustType
    })
    message.success('校正成功')
    dialogVisible.value = false
    emit('success')
  } finally {
    formLoading.value = false
  }
}
</script>
